/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 9, 2013  4:53:52 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.database.dao.staging.impl
 * File Name : FiInvReceiptOutbDaoImpl.java
 * TODO
 */
package com.tis.database.dao.staging.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import promis.utility.CommonService;

import com.tis.database.dao.impl.AbstractDaoImpl;
import com.tis.database.dao.staging.FiInvReceiptOutbDao;
import com.tis.database.entitys.SapMessage;
import com.tis.database.entitys.staging.FiInvReceiptOutb;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 9, 2013 : 4:53:52 PM 
 * CreateBy=Chatree Siangklom
 * 
 * FiInvReceiptOutbDaoImpl
 * TODO
 */
@Repository
@SuppressWarnings("unchecked")
public class FiInvReceiptOutbDaoImpl extends AbstractDaoImpl<FiInvReceiptOutb, String> implements FiInvReceiptOutbDao {

	private static final Logger logging = LoggerFactory.getLogger(FiInvReceiptOutbDaoImpl.class);
	
	/**
	 * Date =Oct 9, 2013 : 4:54:20 PM 
	 * CreateBy=Chatree Siangklom
	 * FiInvReceiptOutbDaoImpl
	 * TODO
	 * @param entityClass
	 */
	protected FiInvReceiptOutbDaoImpl() {
		super(FiInvReceiptOutb.class);
	}

	/**
	 * (Javadoc)
	 * @see com.tis.database.dao.staging.FiInvReceiptOutbDao#Search(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, int)
	 * Date =Oct 11, 2013 : 2:04:22 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public List<FiInvReceiptOutb> Search(	String id, 
											String sapstagingId,
											String company,
											String project,
											String documentNo,
											String refDocNo,
											String accountCode,
											String accountCode2,
											String unitNo,
											String status,	
											String instanceNo,
											String syncIssync,
											String interfaceOnStart, 
											String interfaceOnEnd,
											int maxResult) throws HibernateException, Exception {
		try {
			
			Session session = getCurrentSession();
			Query q = session.getNamedQuery(FiInvReceiptOutb.findBySearch);
			String query = q.getQueryString();
			
			if(!CommonService.IsNull(id))
				query += " And upper(c.id) like '%"+ CommonService.IsNullRString(id).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(company))
				query += " And upper(c.company) like '%"+ CommonService.IsNullRString(company).toUpperCase() +"%' ";
			
			if(!CommonService.IsNull(project))
				query += " And upper(c.project) like '%"+ CommonService.IsNullRString(project).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(documentNo))
				query += " And upper(c.documentNo) like '%"+ CommonService.IsNullRString(documentNo).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(refDocNo))
				query += " And upper(c.refDocNo) like '%"+ CommonService.IsNullRString(refDocNo).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(unitNo))
				query += " And upper(c.unitNo) like '%"+ CommonService.IsNullRString(unitNo).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(accountCode))
				query += " And upper(c.accountCode) like '%"+ CommonService.IsNullRString(accountCode).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(accountCode2))
				query += " And upper(c.accountCode2) like '%"+ CommonService.IsNullRString(accountCode2).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(status))
				query += " And upper(c.status) like '%"+ CommonService.IsNullRString(status).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(instanceNo))
				query += " And upper(c.instanceNo) like '%"+ CommonService.IsNullRString(instanceNo).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(syncIssync))
				query += " And upper(c.syncIssync) like '%"+ CommonService.IsNullRString(syncIssync).toUpperCase() +"%'";
			
			if(!CommonService.IsNull(interfaceOnStart) && !CommonService.IsNull(interfaceOnEnd))
				query += " And to_char(c.syncCreatedon, 'YYYY-MM-DD') BETWEEN :interfaceOnStart And :interfaceOnEnd ";
			
			query += " Order by :p_orderby ";
			
			logging.info(query);
			
			q = session.createQuery(query);
			
			if(!CommonService.IsNull(interfaceOnStart))
				q.setParameter("interfaceOnStart", CommonService.IsNullRString(interfaceOnStart).toUpperCase());
			
			if(!CommonService.IsNull(interfaceOnEnd))
				q.setParameter("interfaceOnEnd", CommonService.IsNullRString(interfaceOnEnd).toUpperCase());
			
			q.setParameter("p_orderby", "c.id desc");
			
			if(maxResult > 0)
				q.setMaxResults(maxResult);			
			
			List<FiInvReceiptOutb> lst = Collections.checkedList(q.list(), FiInvReceiptOutb.class);
			if(CommonService.getSize(lst) > 0 )
				return lst;
			else 
				return null;
			
		}catch(HibernateException ex) {
			logging.error(ex.getMessage());
			ex.printStackTrace();
			throw ex;
		}
		catch(Exception ex1) {
			logging.error(ex1.getMessage());
			ex1.printStackTrace();
			throw ex1;
		}
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.lang.Object)
	 * Date =Oct 9, 2013 : 4:56:29 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(FiInvReceiptOutb e) throws HibernateException {
		super.saveOrUpdate(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#saveOrUpdate(java.util.List)
	 * Date =Oct 9, 2013 : 4:56:34 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void saveOrUpdate(List<FiInvReceiptOutb> e) throws HibernateException {
		super.saveOrUpdate(e);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#findById(java.io.Serializable)
	 * Date =Oct 9, 2013 : 4:57:31 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public FiInvReceiptOutb findById(String id) throws HibernateException {
		return super.findById(id);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#NextSequence(java.lang.String)
	 * Date =Oct 9, 2013 : 4:57:36 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public BigDecimal NextSequence(String name) throws HibernateException, Exception {
		if(CommonService.IsNull(name))
			name = "FI_INV_RECEIPT_OUTB";
		return super.NextSequence(name);
	}
	
	/** (Javadoc)
	 * @see com.tis.database.dao.impl.AbstractDaoImpl#NextInstanceNo()
	 * Date =Oct 14, 2013 : 3:35:15 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public BigDecimal NextInstanceNo() throws HibernateException, Exception {
		return super.NextInstanceNo();
	}
	

	/** (Javadoc)
	 * @see com.tis.database.dao.staging.FiInvReceiptOutbDao#UpdateBackSapMessage(java.util.List)
	 * Date =Oct 14, 2013 : 4:49:08 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public List<FiInvReceiptOutb> UpdateBackSapMessage(List<SapMessage> sapMessages) throws HibernateException, Exception {
		List<FiInvReceiptOutb> lst = new ArrayList<FiInvReceiptOutb>();
		try{
			if(CommonService.getSize(sapMessages) > 0){				
				for(SapMessage value : sapMessages){
					FiInvReceiptOutb obj = findById(value.getId());
					if(obj != null){
						obj.setSapStagingId(value.getSapStagingId());
						
						obj.setMsgCode(value.getMsgCode());
						obj.setMsgDesc(value.getMsgDesc());
						obj.setExtRefKey(value.getExtRefKey());
						obj.setExtRefNo(value.getExtRefNo());																					
						obj.setMsgAltDesc(value.getMsgAltDesc());
						obj.setSyncIssync(value.getSyncIssync());
						obj.setSyncUpdatedby(value.getSyncUpdatedby());
						obj.setSyncUpdatedon(value.getSyncUpdatedon());
						lst.add(obj);
					}
				}
				
				if(CommonService.getSize(lst) > 0) saveOrUpdate(lst); else lst = null;
			}
		}catch(Exception e){
			lst = null;
			e.printStackTrace();
			throw e;
		}
		return lst;
	}

	/** (Javadoc)
	 * @see com.tis.database.dao.staging.FiInvReceiptOutbDao#NextSequence()
	 * Date =Oct 16, 2013 : 10:31:28 AM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public BigDecimal NextSequence() throws HibernateException, Exception {

		return super.NextSequence("FI_INV_RECEIPT_OUTB");
	}
	
	
}
